﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：静态页面管理
  //创建人：赵国伟
  //生成时间：2012/03/24 12:45:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;


namespace Shopping.BLL
{
    
    public class PagesManage
    {
        private readonly DALPages dalPages = new DALPages();
        private readonly DALPageParams dalParams = new DALPageParams();
        private readonly DALPageContents dalContents = new DALPageContents();

        #region "保存静态页面信息；by 赵国伟 2012-03-24"
        /// <summary>
        /// 保存静态页面信息；
        /// </summary>
        /// <returns></returns>
        public ReturnValue SavePage(PageInfo p)
        {
            ReturnValue ret = new ReturnValue();
            if (dalPages.QueryList(1, "PageName='" + p.Page.PageName + "' and PageID<>" + p.Page.PageID, "") != null)
            {
                ret.Message = "页面名称重复！";
                return ret;
            }
            p.Page.CreateDate = DateTime.Now;
            p.Page.LastModifyDate = DateTime.Now;

            if (p.Page.PageID > 0)
            {
                Pages o = dalPages.GetModel(p.Page.PageID);
                p.Page.CreateDate = o.CreateDate;
            }
            return dalPages.SavePage(p);
        }
        #endregion


        #region "更新页面内容 by 赵国伟 2012-03-24"
        /// <summary>
        /// 更新页面内容
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        public ReturnValue SaveContent(List<PageContents> contents)
        {
            ReturnValue ret = dalContents.SaveContent(contents);
            if (ret.IsSucc)
            {
                //生成页面内容；
                ret = GeneratePage(contents);
            }
            return ret;
        }
        #endregion

        #region "生成静态页面； by 赵国伟 2012-03-24"
        /// <summary>
        /// 生成静态页面；
        /// </summary>
        /// <returns></returns>
        public ReturnValue GeneratePage(List<PageContents> contents)
        {
            ReturnValue ret = new ReturnValue();
            PageInfo p = GetPage(contents[0].PageID,false);
            if (p == null)
            {
                ret.Message = "为找到对应的页面信息！";
                return ret;
            }
            string templateFile = System.Web.HttpContext.Current.Server.MapPath("/Templates/" + p.Page.TemplateUrl);
            string targetFile = System.Web.HttpContext.Current.Server.MapPath("/" + p.Page.PageUrl);
            if(!File.Exists(templateFile))
            {
                ret.Message = "模板文件不存在！";
                return ret;
            }
            if (!File.Exists(targetFile))
            {
                ret.Message = "目标文件不存在！";
                return ret;
            }

            string templateContent =  Utility.ReadFile(templateFile);
            foreach (PageContents c in contents)
            {
                templateContent = templateContent.Replace("{Param" + c.ParamNO + "}", c.ParamContent);
            }
            Utility.WriteToFileEx(targetFile, templateContent,"utf-8");
            DALHelper.SetFieldValue<Pages>(contents[0].PageID, PagesEnum.LastModifyDate, DateTime.Now.ToString());
            ret.Result = ResultType.Success;
            ret.Message = "更新成功！";
            return ret;
        }
        #endregion

        #region "查询一条指定的静态页面信息； by 赵国伟 2012-04-15"
        /// <summary>
        /// 查询一条指定的静态页面信息；
        /// </summary>
        /// <param name="pageId"></param>
        /// <param name="IsLoadContent"></param>
        /// <returns></returns>
        public PageInfo GetPage(int pageId,bool IsLoadContent)
        {
            Pages s = dalPages.GetModel(pageId);
            PageInfo p = null;
            if (s != null)
            {
                p = new PageInfo();
                p.Page = s;
                p.Params = dalParams.QueryList("PageID=" + s.PageID, "ParamNO");
                if (IsLoadContent)
                    p.Contents = dalContents.QueryList("PageID=" + s.PageID, "ParamNO");
            }
            return p;
        }
        #endregion

        #region "返回所有的静态页面列表   by 赵国伟 2012-04-15"
        /// <summary>
        /// 返回所有的静态页面列表
        /// </summary>
        /// <returns></returns>
        public List<Pages> QuerList()
        {
            return dalPages.QueryList("", "OrderValue,PageID desc");
        }
        #endregion

        #region "删除一条指定的页面信息   by 赵国伟 2012-04-15"
        /// <summary>
        /// 删除一条指定的页面信息
        /// </summary>
        /// <param name="pageId"></param>
        /// <returns></returns>
        public int DeletePage(int pageId)
        {
            return dalPages.Delete(pageId);
        }
        #endregion


    }
}
